#include <iostream>
#include <string>
#include <memory>

// 基类 Character
class Character {
public:
    virtual void attack() const = 0; // 纯虚函数
    virtual ~Character() {
        std::cout << "Character destroyed." << std::endl;
    } // 虚析构函数
};

// Mage 派生类，拥有魔法书资源
class Mage : public Character {
private:
    std::string magicBook;
public:
    Mage(const std::string& book) : magicBook(book) {}
    void attack() const override {
        std::cout << "Mage casts a spell from " << magicBook << "!" << std::endl;
    }
    ~Mage() {
        std::cout << "Mage's magic book " << magicBook << " is returned to the library." << std::endl;
    }
};

// Warrior 派生类，拥有一把剑
class Warrior : public Character {
private:
    std::string sword;
public:
    Warrior(const std::string& swordName) : sword(swordName) {}
    void attack() const override {
        std::cout << "Warrior attacks with the sword " << sword << "!" << std::endl;
    }
    ~Warrior() {
        std::cout << "Warrior's sword " << sword << " is stored back in the armory." << std::endl;
    }
};

void performAttack(const Character* character) {
    character->attack();
}

int main() {
    std::unique_ptr<Character> mage(new Mage("Ancient Grimoire"));
    std::unique_ptr<Character> warrior(new Warrior("Excalibur"));

    performAttack(mage.get());
    performAttack(warrior.get());

    // Objects are automatically destroyed and resources are freed when the unique_ptr goes out of scope
    return 0;
}